package com.syz.mall.service.impl;

import com.syz.mall.mapper.CollectMapper;
import com.syz.mall.mapper.UserAddressMapper;
import com.syz.mall.mapper.UserMapper;
import com.syz.mall.mapper.UserOtherDataMapper;
import com.syz.mall.pojo.*;
import com.syz.mall.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserOtherDataMapper userOtherDataMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private CollectMapper collectMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public Boolean register(String username, String password) {
        if(userMapper.selectByUsernameUser(username)!=null){
            return false;
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        int count=userMapper.insertSelective(user);
        return count==1;
    }

    @Override
    public Boolean changePassword(Integer id, String newPassword) {
        User user=userMapper.selectByPrimaryKey(id);
        User u=new User();
        u.setId(id);
        u.setPassword(newPassword);
        int count=userMapper.updateByPrimaryKeySelective(u);
        return count==1;
    }

    @Override
    public Boolean checkPassword(Integer id, String oldPassword) {
        User user=userMapper.selectByPrimaryKey(id);
        if(oldPassword.equals(user.getPassword())){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean addAddress(UserAddress userAddress) {
        int count=userAddressMapper.insert(userAddress);
        return count==1;
    }

    @Override
    public Boolean deleteAddressById(Integer id) {
        int count=userAddressMapper.deleteByPrimaryKey(id);
        return count==1;
    }

    @Override
    public Boolean updateAddress(UserAddress userAddress) {
        int count=userAddressMapper.updateByPrimaryKeySelective(userAddress);
        return count==1;
    }

    @Override
    public List<UserAddress> selectAllAddressByUserId(Integer userId) {
        return  userAddressMapper.selectAllByUserId(userId);
    }

    @Override
    public Boolean updateOtherDataSelective(UserOtherData userOtherData) {
//        UserOtherData otherData=userOtherDataMapper.selectByPrimaryKeyByUserId(userOtherData.getUserId());
//        if(otherData==null){
//            otherData=new UserOtherData();
//            otherData.setUserId(userOtherData.getUserId());
//            userOtherDataMapper.insertSelective(otherData);
//        }
        int count = userOtherDataMapper.updateByPrimaryKeySelectiveByUserId(userOtherData);
        return count==1;
    }

    @Override
    public Integer queryIdByUsername(String username) {
        Integer id=userMapper.queryIdByUsername(username);
        return id;
    }

    @Override
    public UserOtherData selectOtherDataByUserId(Integer userId) {
        UserOtherData otherData=userOtherDataMapper.selectByPrimaryKeyByUserId(userId);
        int count=0;
        if(otherData==null){
            otherData=new UserOtherData();
            otherData.setUserId(userId);
            //初始值
            otherData.setNickname("用户"+userId.intValue());
            otherData.setGender(0);
            count=userOtherDataMapper.insertSelective(otherData);
            if(count==1){
                return otherData;
            }else{
                return null;
            }
        }else{
            return otherData;
        }
    }

    @Override
    public UserOtherData selectOtherDataByAnswer(Integer userId, String answer) {
        return userMapper.selectOtherDataByAnswer(userId,answer);
    }

    @Override
    public Boolean collectProduct(Integer userId, Integer productId) {
        if(this.checkIfCollect(userId,productId)){
            return true;
        }
        Collect collect=new Collect();
        collect.setUserId(userId);
        collect.setProductId(productId);
        int count=collectMapper.insertSelective(collect);
        return count==1;
    }

    @Override
    public Boolean checkIfCollect(Integer userId, Integer productId) {
        Collect collect=collectMapper.checkIfCollect(userId,productId);
        return collect!=null;
    }

    @Override
    public Boolean cancelCollect(Integer userId, Integer productId) {
        int count=collectMapper.deleteByUserIdAndProductId(userId,productId);
        return count>=1;
    }

    @Override
    public List<Product> selectUserCollect(Integer userId) {
        return collectMapper.selectUserCollect(userId);
    }


}
